<?php

/**
 * Description of score
 *
 * @author fabrice
 */
class Model_Score extends PDO_Mysql {
    
    protected $_name = 'score';
    protected $_primary = 'id_score';

    /**
     * Recupere tout les scores par utilisateur
     * @return <array>
     */
    public function getscoreByTypescoreId($id_score) {

        $sql = "SELECT * FROM " . $this->_name ." WHERE id_score=" . $id_score;
        $result = $this->pquery($sql);
        $row = $this->pfetchAll($result);
        return $row;
    }
    

    /**
     * Insert user_score
     * @param <int> $id_score
     * @param <int> $id_user
     * @return <bool> $row
     */
    public function setScore($formData) {

        $sql = 'INSERT INTO ' . $this->_name . ' (id_score,id_user,score)' .
                ' VALUES (:id_score,:id_user,:score)';
        $data = array(
            ':id_score' => NULL,
            ':id_user' => $formData['id_user'],
            ':score' => $formData['score']
        );
        $row = $this->pinsert($sql, $data, 'id_score');
        return $row;
    }
    
    public function updateScore($id_user, $score) {

        $sql = 'UPDATE ' . $this->_name . ' SET score=score+? WHERE id_user=' . $id_user;
        $data = array($score);
        $row = $this->pupdate($sql, $data, 'id_score');
        return $row;
    }
    
    public function checkScoreExist($id_user) {

        $sql = 'SELECT COUNT(*) as C FROM score WHERE id_user='.$id_user;
        $query = $this->pquery($sql);
        $row = $this->pfetchObj($query);
        return $row->C;
    }
    
    public function getUserScore($id_user, $count=false){
        $sql = "SELECT score " ;
        if($count)
            $sql .= "/5";
        $sql .= " as score FROM " . $this->_name ." WHERE id_user=" . $id_user;
        $result = $this->pquery($sql);
        $row = $this->pfetchObj($result);
        return $row->score;
    }
    
    public function getUserTotalResponse($id_user){
        
        $sql = "SELECT * FROM user_response as total_response FROM " . $this->_name ." WHERE id_user=" . $id_user;
        $result = $this->pquery($sql);
        $row = $this->pfetchObj($result);
        return $row->score;
    }
}

?>